package edu.pku.sei.transformation.editor.model.constraint;

import org.w3c.dom.Element;

import edu.pku.sei.gefcommon.descriptor.annotation.EnumerationProperty;
import edu.pku.sei.gefcommon.descriptor.annotation.MethodProperty;
import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.xml.XMLUtility;
import edu.pku.sei.transformation.editor.model.ModelPackage;

public class Constraint extends MetaModelElement {
	private String content = "";
	private String language = LANGUAGE_OCL;
	public static String LANGUAGE_OCL = "ocl";
	public static String LANGUAGE_TEXT = "text";
	public static String LANGUAGE_JAVA = "Java";
	private ConstraintConnection connection;
	
	public ConstraintConnection getConnection() {
		return connection;
	}


	public void setConnection(ConstraintConnection connection) {
		this.connection = connection;
	}

	@MethodProperty(catalog="constraint", id=ModelPackage.CONSTRAINT_CONTENT, name="content", setter="setContent")
	public String getContent() {
		return content;
	}
	

	public void setContent(String content) {
		if (this.content == null || !this.content.equals(content)) {
			String oldValue = this.content;
			this.content = content;
			firePropertyChange(ModelPackage.CONSTRAINT_CONTENT, oldValue, this.content);
		}
	}
	
	public void setLanguage(String s) {
		if (this.language == null || !language.equals(s)) {
			language = s;
		}
	}
	@MethodProperty(id = ModelPackage.CONSTRAINT_LANGUAGE, catalog = "constraint", name = "language", setter="setLanguage")
	@EnumerationProperty(function="getLanguages")
	public String getLanguage() {
		if (language == null)
			language = LANGUAGE_OCL;
		return language;
	}
	
	public String[] getLanguages() {
		return new String[]{LANGUAGE_OCL, LANGUAGE_TEXT, LANGUAGE_JAVA};
	}
	
	@Override
	public void serialize(Element root) {
		super.serialize(root);
		XMLUtility.xmlInstance.serializeAttribute(root, ModelPackage.CONSTRAINT_CONTENT, content);
		XMLUtility.xmlInstance.serializeAttribute(root, ModelPackage.CONSTRAINT_LANGUAGE, language);
		XMLUtility.xmlInstance.serializeNodeNotContain(root, ModelPackage.CONSTRAINT_CONNECTION, connection);
	}
	
	@Override
	public void deSerialize(Element root) {
		super.deSerialize(root);
		this.content = XMLUtility.xmlInstance.deSerializeString(root, ModelPackage.CONSTRAINT_CONTENT);
		this.language = XMLUtility.xmlInstance.deSerializeString(root, ModelPackage.CONSTRAINT_LANGUAGE);
		this.connection = (ConstraintConnection) XMLUtility.xmlInstance.deSerializeNodeNotContain(root, ModelPackage.CONSTRAINT_CONNECTION, this);
	}
	
	@Override
	public void dynamicSet(String featureID, Object value) {
		if (ModelPackage.CONSTRAINT_CONNECTION.equals(featureID)) {
			this.connection = (ConstraintConnection) value;
		} else {
			super.dynamicSet(featureID, value);
		}
	}
}
